package com.mumu.jetpackdemo.viewmodel;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mumu.jetpackdemo.R;

/**
 * - @Description: viewModel使用<br/>
 * - @Author: edie<br/>
 * - @Time: 2019/3/26 上午10:02
 */
public class DemoVIewModelActivity extends AppCompatActivity implements View.OnClickListener {

    private int tag;
    private TextView helloTv;
    private Button button2;
    private MyViewModel mViewModel;

    public static void startViewModel(Context context) {
        context.startActivity(new Intent(context, DemoVIewModelActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_view_model);
        initView();
        mViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
        upHello();
    }

    private void initView() {
        helloTv = (TextView) findViewById(R.id.hello_tv);
        button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(this);
    }

    private void upHello() {
        helloTv.setText(mViewModel.getHello());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                mViewModel.setHello("变更-" + tag++);
                upHello();
                break;
        }
    }
}
